﻿using Kingdee.BOS;
using Kingdee.BOS.App.Core;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Log;
using Kingdee.BOS.Msg;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Newtonsoft.Json;
using RK.Model;
using RK.Model.Ronma;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace RK
{
    public static class Base
    {
        public static string GetAllocateJson(int pkIds, string tOrgIds)
        {
            var param = new
            {
                PkIds = pkIds,
                TOrgIds = tOrgIds,
                IsAutoSubmitAndAudit = true
            };
            return JsonConvert.SerializeObject(param);
        }
        /// <summary>
        /// 提交和审核参数
        /// </summary>
        /// <param name="curBillNo"></param>
        /// <returns></returns>
        public static string GetParam(string curBillNo)
        {
            var param = new
            {
                Numbers = curBillNo
            };
            return JsonConvert.SerializeObject(param);
        }
        /// <summary>
        /// 获取单据或者基础资料
        /// </summary>
        /// <param name="formId">界面标识</param>
        /// <param name="fid">单据ID</param>
        /// <returns></returns>
        public static DynamicObject[] GetDynamicObjectsByFormIdAndWhere(Context context, string formId, string where)
        {
            FormMetadata meta = MetaDataServiceHelper.GetFormMetaData(context, formId);

            QueryBuilderParemeter queryParam = new QueryBuilderParemeter();
            queryParam.FormId = formId;
            List<string> filters = new List<string>();
            if (!where.IsNullOrEmptyOrWhiteSpace())
            {
                filters.Add(where);
            }
            queryParam.FilterClauseWihtKey = string.Join(" and ", filters);
            var objs = BusinessDataServiceHelper.Load(context, meta.BusinessInfo.GetDynamicObjectType(), queryParam);
            return objs;
        }
        /// <summary>
        /// 构造WebApi单据查询参数
        /// </summary>
        /// <param name="formId">单据标识</param>
        /// <param name="fieldKeys">查询的字段名</param>
        /// <param name="filterString">查询过滤条件</param>
        /// <returns></returns>
        public static string QueryParameter(string formId, string fieldKeys, string filterString, string orderString, int limit)
        {
            var param = new
            {
                FormId = formId,
                FieldKeys = fieldKeys,
                FilterString = filterString,
                OrderString = orderString,
                Limit = limit
            };
            return JsonConvert.SerializeObject(param);
        }
        /// <summary>
        /// 构造WebApi单据查询参数
        /// </summary>
        /// <param name="formId">单据标识</param>
        /// <param name="fieldKeys">查询的字段名</param>
        /// <param name="filterString">查询过滤条件</param>
        /// <returns></returns>
        public static string QueryParameter(string formId, string fieldKeys, string filterString)
        {
            var param = new
            {
                FormId = formId,
                FieldKeys = fieldKeys,
                FilterString = filterString
            };
            return JsonConvert.SerializeObject(param);
        }
        /// <summary>
        /// 操作插件添加提示信息
        /// </summary>
        /// <param name="operationResult">当前返回信息提示对象</param>
        /// <param name="successStatus">消息框模式，true是确认，false是错误模式</param>
        /// <param name="message">消息主体</param>
        /// <param name="messageType">提示框样式</param>
        /// <param name="name">消息标题</param>
        /// <param name="pkValue">消息排序号</param>
        public static void AddOperationResult(IOperationResult operationResult, bool successStatus, string message, MessageType messageType, string name, int pkValue)
        {
            IOperationResult opResult = new OperationResult();
            opResult.OperateResult.Add(new OperateResult()
            {
                SuccessStatus = successStatus,
                Message = message,
                MessageType = messageType,
                Name = name,
                PKValue = pkValue,
            });
            operationResult.MergeResult(opResult);
        }
        /// <summary>
        /// 单据状态转换成中文
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetStatus(string str)
        {
            string vuele = string.Empty;
            switch (str)
            {
                case "A":
                    vuele = "创建";
                    break;
                case "B":
                    vuele = "审核中";
                    break;
                case "C":
                    vuele = "已审核";
                    break;
                case "D":
                    vuele = "重新审核";
                    break;
                case "Z":
                    vuele = "暂存";
                    break;
                default:
                    vuele = str;
                    break;
            }
            return vuele;
        }
    }
}
